﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Logger=Garlic.CC.Utility.Logger;

using Garlic.CC.BaseClass;
using Garlic.CC.Utility;
using Garlic.UI.Components;
using Garlic.BLL;
using Garlic.DM.DataModel;
using System.Text;

namespace Garlic
{
    public partial class AdminPortal : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ResetToolbarCommand();
            }
        }

        private void ResetToolbarCommand()
        {
            //重置子页面里的toolbar command string
            GeneralToolBar toolBar = this.ContentPlaceHolder1.FindControl("TopToolBar") as GeneralToolBar;
            if (toolBar == null) return;

            IUser currentUser = UserUtil.GetCurrentUser();
            if (currentUser == null) return;

            string virtualPath = this.Page.Request.ApplicationPath;
            string actionPath = this.Page.Request.FilePath;

            //去除虚拟目录
            if (!string.IsNullOrEmpty(virtualPath) && virtualPath.Length > 1)
            {
                actionPath = actionPath.ToLower().Replace(virtualPath.ToLower(), "");
            }
            if (actionPath.IndexOf('/') == 0) //如果第一字符是'/',则删除
            {
                actionPath = actionPath.Substring(1);
            }
            List<ActionInfo> actions = AuthManager.GXYctions(currentUser, actionPath, "Top");
            if (actions == null)
            {
                toolBar.ResetCommandString("");
                // Logger.LogError("AdminPortal.Master", "ResetToolbarCommand", "AdminPortal.Master", "获取用户" + currentUser.AccountName + "授权动作失败", actionPath);
                return;
                //throw new AuthenticationException("没有授权");
            }

            StringBuilder commandStringBuilder = new StringBuilder();
            foreach (ActionInfo info in actions)
            {
                commandStringBuilder.Append(info.Action_name_cn);
                commandStringBuilder.Append("|");
                commandStringBuilder.Append(info.Action_name);
                commandStringBuilder.Append("|");
                commandStringBuilder.Append(info.Icon_name);
                commandStringBuilder.Append("|");
                commandStringBuilder.Append(info.Client_js);
                commandStringBuilder.Append("|");
                //加入动作类型，具体定义参见
                //XY.BLL.Common.ActionType
                commandStringBuilder.Append(info.Action_type.ToString());
                commandStringBuilder.Append(";");
            }
            toolBar.ResetCommandString(commandStringBuilder.ToString());

            //获取监控页面刷新的时间间隔
            //int MonitorTimeSpan = SysConfigManager.GetMonitorTimeSpan();
            //this.monitor_time_span.Value = MonitorTimeSpan.ToString();
        }

        public void ShowMessage(string msg)
        {
            this.pnlMessage.Visible = true;
            this.lblMessage.Text = msg;
        }

        protected void lbEnterKey_Click(object sender, EventArgs e)
        {

        }

        protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
        {
            Exception ex = e.Exception;
            while ((ex is System.Reflection.TargetInvocationException) && ex.InnerException != null)
            {
                ex = ex.InnerException;
            }
            if (ex.Data["ExtraInfo"] != null)
            {
                ScriptManager1.AsyncPostBackErrorMessage =
                    ex.Message +
                    ex.Data["ExtraInfo"];
            }
            else
            {
                ScriptManager1.AsyncPostBackErrorMessage = ex.Message;
            }

            Logger.LogError("", "ScriptManager1_AsyncPostBackError", "AdminPortal", ScriptManager1.AsyncPostBackErrorMessage);
        }
    }//end class
}